package common.manager
{
	/**
	 * 当Id不为null时单个加载Event事件，
	 * 为null是执行的是BulkProgressEvent事件加载全部的添加的资源
	 * @example
	 * Resource.add("test","test.xml");
	 * Resource.start("test",testFun,null);
	 * public function testFun(evt : Event) : void 
	 * {
	 *	 Resource.stop("test",testFun,null);
	 * } 
	 * 
	 * Resource.start(null,testFun,null);
	 * public function testFun(evt : BulkProgressEvent) : void 
	 * {
	 *	 Resource.stop(null,testFun,null);
	 * } 
	 */	
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;

	/**
	 * 资源管理器
	 * @author liuyong
	 */	
	public class Resource 
	{	
		static private var m_brGameResource : BulkLoader;
		
		public function Resource()
		{
		
		}
		
		static public function add(id:String,url:String,priority:uint=0,applicationDomain:ApplicationDomain=null,weight:int=0) : void
		{
			var loadContext : LoaderContext = new LoaderContext;
			loadContext.applicationDomain = applicationDomain;
			
			if(weight == 0)
			{
				Loader.add(url,{id:id,priority:priority,context:loadContext});
			}
			else
			{
				Loader.add(url,{id:id,priority:priority,context:loadContext,weight:weight});
			}
		}
		
		/**
		 *当Id不为null时单个加载Event事件，为null是执行的是BulkProgressEvent事件加载全部的添加的资源
		 */		
		static public function start(id:String=null,completeFun:Function=null,progressFun:Function=null) : void
		{
			if(id)
			{
				if(completeFun != null)
				{
					Loader.get(id).addEventListener(Event.COMPLETE,completeFun);
				}
				if(progressFun != null)
				{
					Loader.get(id).addEventListener(ProgressEvent.PROGRESS,progressFun);
				}
				
				Loader.reload(id);
			}
			else
			{
				if(completeFun != null)
				{
					Loader.addEventListener(BulkProgressEvent.COMPLETE,completeFun);
				}
				if(progressFun != null)
				{
					Loader.addEventListener(BulkProgressEvent.PROGRESS,progressFun);
				}
				
				Loader.start();
			}
		}
		
		/**
		 *当Id不为null时单个移除Event事件，为null是执行移除的是BulkProgressEvent事件加载全部的添加的资源的
		 */	
		static public function stop(id:String=null,completeFun:Function=null,progressFun:Function=null) : void
		{
			if(id)
			{
				if(completeFun != null)
				{
					Loader.get(id).removeEventListener(Event.COMPLETE,completeFun);
				}
				if(progressFun != null)
				{
					Loader.get(id).removeEventListener(ProgressEvent.PROGRESS,progressFun);
				}
			}
			else
			{
				if(completeFun != null)
				{
					Loader.removeEventListener(BulkProgressEvent.COMPLETE,completeFun);
				}
				if(progressFun != null)
				{
					Loader.removeEventListener(BulkProgressEvent.PROGRESS,progressFun);
				}
			}
		}
		
		static public function clear() : void
		{
			Loader.clear();
		}
		
		static public function getBitmap(key : String,clearMemory:Boolean=false) : Bitmap
		{
			return Loader.getBitmap(key,clearMemory);
		}
		
		public function getBitmapData(key : String,clearMemory:Boolean=false) : BitmapData
		{
			return Loader.getBitmapData(key,clearMemory);
		}
		
		static public function getXML( key : String ,clearMemory:Boolean=false) : XML
		{
			return Loader.getXML( key,clearMemory );
		}
		
		static public function getMovieClip( key : String,clearMemory:Boolean=false ) : MovieClip
		{
			return Loader.getMovieClip( key,clearMemory);
		}
		
		static public function getBinary( key : String,clearMemory:Boolean=false ) : ByteArray
		{
			return Loader.getBinary( key,clearMemory);
		}
		
		static public function getText( key : String,clearMemory:Boolean=false ) : String
		{
			return Loader.getText( key,clearMemory);
		}
		
		static public function getClass(className:String,swfKey:String,clearMemory:Boolean=false) : Class
		{
		    var cs : Class = Loader.getMovieClip(swfKey,clearMemory).loaderInfo.applicationDomain.getDefinition(className) as Class;
			
			return cs;
		}
		
		/**
		 * 
		 * @return  Evt单列
		 * 
		 */	
		static private function get Loader() : BulkLoader
		{
			if(!m_brGameResource)
			{
				m_brGameResource =  new BulkLoader("GameResource");;
			}
			
			return m_brGameResource;
		}
	}
}